<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Send_sms extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		header("Content-type: text/html; Charset=utf-8");
	}
	
	/**
	 * @注册发送验证码
	 * */
	public function index()    
	{
        if (WBE_DOMAIN === 'myuhelper.cn') {
            /**@测试服务器，验证码为123456*/
	        $mobile = $this->input->post('mobile');
	        $setcode['mobile'] = $mobile;
	        $setcode['time']   = time();
	        $setcode['code']   = '123456';
	        $this->input->set_cookie('setcode', base64_encode(json_encode($setcode)), 300);
	        echo json_encode(true);exit;
	    }
	    
	    $mobile = $this->input->post('mobile');
	    /**@1分钟内禁止重发短信*/
	    $had_sms = $this->Base_model->getLike('api_request', array('request'=>$mobile), array('time >'=>strtotime('-1 min')))->num_rows();
	    if ($had_sms > 0) {
	        echo json_encode(false);exit;
	    }
        $mobile_ip = $mobile.'_'.getIp();
        /**@黑名单，同一手机号，同一ip*/
        $black = $this->Base_model->getTableNum('user_black', array('mobile'=>$mobile_ip, 'status'=>2));
        if ($black > 0) {
            echo json_encode(false);exit;
        }
        /**@1天内发送5条短信，加入黑名单*/
        $sms5 = $this->Base_model->getTableNum('api_request', array('request'=>$mobile_ip, 'time >'=>strtotime('-1 day')));
        if ($sms5 == 5) {
            $data['status'] = 2;
            $data['time'] = time();
            $this->Base_model->update('user_black', array('mobile'=>$mobile_ip), $data);
            echo json_encode(false);exit;
        }
        /**@1小时内发送3条短信，加入黑名单-警告*/
        $sms3 = $this->Base_model->getTableNum('api_request', array('request'=>$mobile_ip, 'time >'=>strtotime('-1 hour')));
        if ($sms3 == 3) {
            $data['mobile'] = $mobile_ip;
            $data['status'] = 1;
            $data['time'] = time();
            $this->Base_model->insert('user_black', $data);
        }
        
        $this->load->helper('send');
        $code = rand(100000, 999999);   //短信验证码
        $limit = 5;                    //验证码有效期
        $tempId = 69005;               //短信模板
        $sms = send_sms($code, $limit, $mobile, $tempId);
        if ($sms['status']) {
            $api['status'] = '发送成功';
            $setcode['mobile'] = $mobile;
            $setcode['time']   = $sms['res'];
            $setcode['code']   = $code;
            $this->input->set_cookie('setcode', base64_encode(json_encode($setcode)), 300);
        } else {
            if ($sms['res'] == NULL) {
                $api['status'] = '短信api返回null';
            } else {
                $api['status'] = '错误码：'.$sms['res']['statusCode'].'，错误消息：'.$sms['res']['statusMsg'];
            }
        }
        $api['api']     = getApiList(0);  //短信通知
        $api['request'] = $mobile_ip;
        $api['time']    = time();
        $this->Base_model->insert('api_request', $api);
        echo json_encode($sms['status']);
	}
	
	
	
}
/** End of file Send_sms.php */
/** Location: ./application/controllers/Send_sms.php */